using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisTreeInclude : AnalysisStatement
{
    private WeakReference<AnalysisTree>? _treeReference;

    internal AnalysisTreeInclude(
        SyntaxTreeIncludeNode syntaxNode,
        AnalysisNode? parent) : base(AnalysisBoundKinds.TreeInclude, syntaxNode, parent)
    {
        Syntax = syntaxNode;
    }

    public override SyntaxTreeIncludeNode Syntax { get; }

    public override int ChildrenCount => 0;

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return [];
    }

    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisTreeInclude cannot have children.");
    }

    public void UpdateAnalysisTree(AnalysisTree analysisTree)
    {
        _treeReference = new WeakReference<AnalysisTree>(analysisTree);
    }


    public AnalysisTree? GetAnalysisTree()
    {
        return _treeReference?.TryGetTarget(out AnalysisTree? analysisTree) == true
            ? analysisTree
            : null;
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个#include节点
    /// </summary>
    /// <param name="syntaxTree">原始语法树</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">语法节点所在的With语句上下文类型符号</param>
    public static AnalysisTreeInclude TreeInclude(SyntaxTreeIncludeNode syntaxTree, AnalysisNode? parent,
        ITypeSymbol? withSymbol)
    {
        return new AnalysisTreeInclude(syntaxTree, parent).WithContainingWithSymbol(withSymbol);
    }
}